  /******************************************************************************
  * @file    PT32X005_wdg.c
  * @author  应用开发团队
  * @version V1.1.0
  * @date    2020/1/27
  * @brief    This file provides firmware functions to manage the following
  *          functionalities of the Independent watchdog (IWDG) peripheral:
  *           + Prescaler and Counter configuration
  *           + IWDG activation
  *           + Flag management
  *
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "PT32X005_wdg.h"

/** @defgroup WDG
  * @brief WDG driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Enables or disables write access all registers.
  * @param  WDGx:select the WDG peripheral.
  * @param  SEL: new state of write access to all registers.
  *          This parameter can be one of the following values:
  *            @arg WDG_LOCK_UnLock: Enable write access to all registers
  *            @arg WDG_LOCK_Lock: Disable write access to all registers
  * @retval None
  */
void WDG_LockConfig(IWDG_TypeDef* WDGx, u32 SEL)
{
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	assert_param(IS_WDG_LOCK_SEL(SEL));
	IWDG->LOCK = SEL;
}


/**
  * @brief  Get the IWDG lock status.
  * @param  WDGx:select the WDG peripheral.
  * @retval The new state of Lock (SET or RESET).
  */
FlagStatus WDG_GetLockStatus(IWDG_TypeDef* WDGx)
{
	FlagStatus bitstatus = RESET;
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	if ((IWDG->LOCK & IWDG_LOCK_Status) != (u32)RESET)
	{
		bitstatus = SET;
	}
	else
	{
		bitstatus = RESET;
	}
	/* Return the flag status */
	return bitstatus;
}

/**
  * @brief  Sets IWDG Reload value.
  * @param  WDGx:select the WDG peripheral.
  * @param  Reload: specifies the IWDG Reload value.
  *          This parameter must be a number between 0 and 0xFFFFFFFF.
  * @retval None
  */
void WDG_SetReload(IWDG_TypeDef* WDGx, u32 Reload)
{
	/* Check the parameters */
	assert_param(IS_WDG_RLR_VAL(Reload));
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	IWDG->RLR = Reload;
}

/**
  * @brief  Reloads IWDG counter
  * @param  WDGx:select the WDG peripheral.
  * @retval None
  */
void WDG_ReloadCounter(IWDG_TypeDef* WDGx)
{
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	IWDG->KR = 1;
}

/**
  * @brief  Enable or Disable WDGx
  * @param  WDGx:select the WDG peripheral.
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void WDG_Cmd(IWDG_TypeDef* WDGx, FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	if (NewState != DISABLE)
	{
		/* Enable the IWDG */
		IWDG->CR |= IWDG_CR_EN;
	}
	else
	{
		/* Disable the IWDG */
		IWDG->CR &= ~IWDG_CR_EN;
	}
}

/**
  * @brief   Enables or disables the specified WDG Reset.
  * @param  WDGx:select the WDG peripheral.
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void WDG_ResetConfig(IWDG_TypeDef* WDGx, FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	if (NewState != DISABLE)
	{
		/* Enable the IWDG RESET */
		IWDG->CR |= IWDG_CR_RSTE;
	}
	else
	{
		/* Disable the IWDG RESET*/
		IWDG->CR &= ~IWDG_CR_RSTE;
	}
}

/**
  * @brief   Enables or disables the specified WDG DebugPending
  * @param  WDGx:select the WDG peripheral.
  * @param  NewState: This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void WDG_DbgPendingConfig(IWDG_TypeDef* WDGx, FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	if (NewState != DISABLE)
	{
		/* Enable the IWDG RESET */
		IWDG->CR |= IWDG_CR_DBGE;
	}
	else
	{
		/* Disable the IWDG RESET*/
		IWDG->CR &= ~IWDG_CR_DBGE;
	}
}

/**
  * @brief  Checks whether the specified IWDG flag is set or not.
  * @param  WDGx:select the WDG peripheral.
  * @param  IWDG_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *            @arg IWDG_SR_HDF
  * @retval The new state of IWDG_FLAG (SET or RESET).
  */
FlagStatus WDG_GetFlagStatus(IWDG_TypeDef* WDGx, u16 WDG_FLAG)
{
	FlagStatus bitstatus = RESET;
	/* Check the parameters */
	assert_param(IS_WDG_SR_SEL(WDG_FLAG));
	assert_param(IS_WDG_ALL_PERIPH(WDGx));
	if ((IWDG->SR & WDG_FLAG) != (u32)RESET)
	{
		bitstatus = SET;
	}
	else
	{
		bitstatus = RESET;
	}
	/* Return the flag status */
	return bitstatus;
}




/**
  * @}
  */

